package cn.yang.domain.activity.service.quota.rule.impl;

import cn.yang.domain.activity.model.entity.ActivityCountEntity;
import cn.yang.domain.activity.model.entity.ActivityEntity;
import cn.yang.domain.activity.model.entity.ActivitySkuEntity;
import cn.yang.domain.activity.model.valobj.ActivityStateVO;
import cn.yang.domain.activity.service.quota.rule.AbstractActionChain;
import cn.yang.types.enums.ResponseCode;
import cn.yang.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @version v1.0
 * @ClassName: cn.yang.domain.activity.service.rule.impl
 * @Description: 活动规则过滤【日期，状态】
 * @Author: YJ
 */

/**
 * 将该类注册为 Spring Bean，名称为"activity_base_action"，
 * 名称与枚举类ActionModel中的code字段对应，实现依赖注入时的按名称匹配
 */
@Slf4j //日志
@Component("activity_base_action")
public class ActivityBaseActionChain extends AbstractActionChain {
    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        log.info("活动责任链-基础信息 【有效期，状态】 校验开始。sku:{} activityId:{}",
                activitySkuEntity.getSku(), activityEntity.getActivityId());
        // 1.活动状态校验
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(),
                    ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        // 2. 校验：活动日期【开始时间<-当前时间->结束时间】
        Date currentDate = new Date();
        if (activityEntity.getBeginDateTime().after(currentDate) || activityEntity.getEndDateTime().before(currentDate)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(),
                    ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }
        //3. 校验：活动库存
        if(activitySkuEntity.getStockCountSurplus()<=0){
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(),
                    ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }
        return next().action(activitySkuEntity, activityEntity, activityCountEntity);
    }
}
